package com.example.q_kang.hhh.message;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bulong.rudeness.RudenessScreenHelper;
import com.example.q_kang.hhh.R;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.base.MVPFragment;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.base.App;
import com.sskj.base.bean.MessageListBean;
import com.sskj.base.bean.SendListBean;
import com.sskj.base.mvchelper.ModelRx2DataSource;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.base.view.ToolBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Flowable;

/**
 * @author Hey
 * Create at  2019/04/09
 */
public class MessageFragment extends MVPFragment<MessagePresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter<MessageListBean.MessageBean> slimAdapter;
    private MVCCoolHelper<List<MessageListBean.MessageBean>> mvcHelper;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    public MessagePresenter getPresenter() {
        return new MessagePresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (mvcHelper != null) {
                mvcHelper.refresh();
            }
        }
    }

    @Override
    public void initView() {
        toolbar.setRightButtonOnClickListener(view -> {
            showLoading();
            mPresenter.deleAllMsg();
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(App.INSTANCE));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(App.INSTANCE)
                .setDividerColor(Color.TRANSPARENT)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_notice, new SlimInjector<MessageListBean.MessageBean>() {
            @Override
            public void onInject(MessageListBean.MessageBean data, IViewInjector injector) {
                injector.clicked(R.id.cl_msg, view -> MsgDetailActivity.start(getActivity(), data.getRec_id()))
                        .text(R.id.tv_date, TextUtils.isEmpty(data.getSend_time()) ? "" :
                                DateUtil.getDateFormat(Long.valueOf(data.getSend_time()) * 1000, DateUtil.PATTERN_J))
                        .text(R.id.tv_msg, data.getMessage());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<MessageListBean.MessageBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<MessageListBean.MessageBean>)
                        page -> mPresenter.getMessageList(page), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();

//        Flowable.rangeLong(0, 8)
//                .map(i -> i + "")
//                .toList()
//                .subscribe(strings -> slimAdapter.updateData(strings));
    }


    @Override
    public void initData() {

    }


    public static MessageFragment newInstance() {
        MessageFragment fragment = new MessageFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void deleteAllSuccess() {
        mvcHelper.refresh();
    }
}
